package com.yl.chainStore.control.system


import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.pay.SysCompanyPayItem
import com.yl.chainStore.entity.pay.SysCompanyPayItemType
import com.yl.chainStore.pojo.vo.pay.SystemCompanyPayItemSelectVo
import com.yl.chainStore.services.pay.ISysCompanyPayItemService
import com.yl.chainStore.services.pay.ISysCompanyPayItemTypeService
import com.yl.chainStore.services.IWebSystemService
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

/**
 * 系统收费项目视图
 */
@RestController
@RequestMapping("sys/pay/item")
open class SysCompanyPayItemController(

	private val iWebSystemService: IWebSystemService,

	private val iSysCompanyPayItemTypeService: ISysCompanyPayItemTypeService,

	private val iSysCompanyPayItemService: ISysCompanyPayItemService
) : BaseCtrl() {

	/**
	 * 获取分类树
	 */
	@GetMapping("getTypeTree")
	@Transactional
	open fun getTypeTree(): ResponseBody {
		return returnSuccess(iWebSystemService.getItemTypeTree())
	}

	/**
	 * 添加一个共有的类型
	 */
	@PostMapping("addType")
	@Transactional
	open fun getTypeTree(@RequestBody type: SysCompanyPayItemType): ResponseBody {
		return iWebSystemService.addItemType(type)
	}

	/**
	 * 修改类型信息
	 */
	@PostMapping("updateType")
	@Transactional
	open fun updateType(@RequestBody type: SysCompanyPayItemType): ResponseBody {
		return iSysCompanyPayItemTypeService.update(type)
	}

	/**
	 * 删除分类
	 */
	@PostMapping("delType/{id}")
	@Transactional
	open fun delType(@PathVariable id: Long): ResponseBody {
		return iSysCompanyPayItemTypeService.delType(id)
	}

	/**
	 * 服务项目分页
	 */
	@PostMapping("itemPage")
	@Transactional
	open fun itemPage(@RequestBody vo: SystemCompanyPayItemSelectVo):ResponseBody {
		return returnSuccess( iWebSystemService.getPayItemPage(Page(vo.current,vo.size),vo))
	}

	/**
	 * 添加项目
	 */
	@PostMapping("addItem")
	@Transactional
	open fun addItem(@RequestBody item:SysCompanyPayItem):ResponseBody{
		return iWebSystemService.addPayItem(item)
	}

	/**
	 * 修改项目
	 */
	@PostMapping("updateItem")
	@Transactional
	open fun updateItem(@RequestBody item:SysCompanyPayItem):ResponseBody{
		return iSysCompanyPayItemService.updateItem(item)
	}

	/**
	 * 删除项目
	 */
	@PostMapping("delItem/{id}")
	@Transactional(rollbackFor = [Exception::class])
	open fun delItem(@PathVariable id : Long):ResponseBody{
		val res = iSysCompanyPayItemService.delItem(id)
		if(res.code != com.yl.common.ErrorCode.SUCCESS.code){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return res
	}


}
